package com.fg.cloud.manager.service.report.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.order.vo.AreaOrderReportVo;
import com.fg.cloud.manager.entry.order.vo.OrderDetailReportVo;
import com.fg.cloud.manager.entry.report.AmountEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.report.OrderReportMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.report.OrderReportService;
import com.google.common.collect.Lists;

@Service
public class OrderReportServiceImpl extends BaseServiceImpl implements OrderReportService {

	@Autowired
	private OrderReportMapper orderReportMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return orderReportMapper;
	}

	/**
	 * 统计订单销量报表
	 */
	@Override
	public List<OrderDetailReportVo> findReport(Map<String, Object> condition) {
		//查询指定经销商区域的总销量
		BigDecimal total = orderReportMapper.findArea(condition);
		BigDecimal hundred = new BigDecimal(100);
		if ( total != null ) {
			List<OrderDetailReportVo> list = orderReportMapper.findReport(condition);
			//当记录数不超过10条
			if ( list.size() <= 10 ) {
				for( int i = 0; i < list.size(); i++ ) {
					//处理value(商品销量百分比)
					BigDecimal value = list.get(i).getTotalAmount().divide(total, 6, BigDecimal.ROUND_HALF_UP).multiply(hundred);		
					list.get(i).setValue( value );
					//处理name(商品名称规格总销量总金额组合)
					if ( StringUtils.isNotEmpty(list.get(i).getProductSpec()) ){
						list.get(i).setProductSpec( "【".concat(
								list.get(i).getProductSpec().replaceAll("[{}\" ]+", "")).concat("】") );
						list.get(i).setSumMoney( list.get(i).getSumMoney().setScale(2, BigDecimal.ROUND_HALF_UP) );
						list.get(i).setName( list.get(i).getProductName().concat(
								list.get(i).getProductSpec()).concat(" 总销量:"+list.get(i).getTotalAmount()).concat(
								" 总销售额:"+list.get(i).getSumMoney()+" 占比:"+String.format("%.2f", value)+"%") );
					}
				}
				return list;
			} else if( list.size() > 10 ) {
				List<OrderDetailReportVo> list2 = Lists.newArrayList();
				//前9条记录正常输出
				for( int i = 0; i < 9; i++ ) {
					//处理value(商品销量百分比)
					BigDecimal value = list.get(i).getTotalAmount().divide(total, 6, BigDecimal.ROUND_HALF_UP).multiply(hundred);		
					list.get(i).setValue( value );
					//处理name(商品名称规格总销量总金额组合)
					if ( StringUtils.isNotEmpty(list.get(i).getProductSpec()) ){
						list.get(i).setProductSpec( "【".concat(
								list.get(i).getProductSpec().replaceAll("[{}\" ]+", "")).concat("】") );
						list.get(i).setSumMoney( list.get(i).getSumMoney().setScale(2, BigDecimal.ROUND_HALF_UP) );
						list.get(i).setName( list.get(i).getProductName().concat(
								list.get(i).getProductSpec()).concat(" 总销量:"+list.get(i).getTotalAmount()).concat(
								" 总销售额:"+list.get(i).getSumMoney()+" 占比:"+String.format("%.2f", value)+"%") );
					}
				}
				//从第10条记录开始处理
				BigDecimal totalAmount = new BigDecimal(0);
				BigDecimal sumMoney = new BigDecimal(0);
				for( int i = 9; i < list.size(); i++ ) {
					totalAmount = totalAmount.add(list.get(i).getTotalAmount());
					sumMoney = sumMoney.add(list.get(i).getSumMoney());
				}
				//处理value(商品销量百分比)
				BigDecimal value2 = totalAmount.divide(total, 6, BigDecimal.ROUND_HALF_UP).multiply(hundred);
				list.get(9).setValue(value2);
				//处理name(商品名称规格总销量总金额组合)
				list.get(9).setName( "其它".concat(" 总销量:"+totalAmount).concat(" 总销售额:"+
										sumMoney).concat(" 占比:"+String.format("%.2f", value2)+"%") );
				for( int i = 0; i < 10; i++  ) {
					list2.add(list.get(i));
				}
				return list2;
			}	
		}
		return Lists.newArrayList();
	}

	@Override
	public List<AmountEntity> findSale(AmountEntity entity) {
		if (1 == entity.getType())
			return orderReportMapper.findSaleByOrder(entity);
		else if (2 == entity.getType())
			return orderReportMapper.findSaleByReturnOrder(entity);
		
		return new ArrayList<AmountEntity>();
	}

	
	/**
	 * 统计区域订单&退货单报表
	 */
	@Override
	public List<AreaOrderReportVo> areaOrderReport(Map<String, Object> condition, 
			String areaNumberSort, Byte type) {
		//设置默认查询的级别
		Integer length = 3;
		//处理区域选择信息的编码拼接查询问题
		if (StringUtils.isNotEmpty(areaNumberSort)) {
			//转换查询条件编码
			areaNumberSort = "0_".concat(areaNumberSort.replaceAll(",", "_"));
			length = areaNumberSort.split("[,_ ]+").length;
		}
		
		List<AreaOrderReportVo> list = null;
		List<AreaOrderReportVo> suppleList = null;
		if ( type == 1 ) {//订单
			list = orderReportMapper.orderQuery(condition, length, areaNumberSort);
			if (length > 2)
				suppleList = orderReportMapper.supplementOrderQuery(condition, length, 
					areaNumberSort);
			
		} else if ( type == 2 ) {//退货单
			list = orderReportMapper.returnOrderQuery(condition, length, areaNumberSort);
			if (length > 2)
				suppleList = orderReportMapper.supplementReturnOrderQuery(condition, length, 
					areaNumberSort);
		}
		
		if (null != suppleList) supplementListData(list, suppleList);
		
		for (int i = 0; i < list.size(); i++) {
			
			list.get(i).setGenre(list.get(i).getLongName().
					concat(" 下单次数："+list.get(i).getFrequency().toString()));
			list.get(i).setSold(list.get(i).getAmountPay());
		}
		return list;
	}

	/**
	 * Description:融合数据
	 * @param baseList
	 * @param suppleList
	 * @author around
	 * @date 2017年10月17日上午11:43:22
	 */
	private void supplementListData(List<AreaOrderReportVo> baseList, 
			List<AreaOrderReportVo> suppleList) {
		boolean flag = true;
		for (AreaOrderReportVo supple : suppleList) {
			for (AreaOrderReportVo entity : baseList) {
				if (supple.getLongName().equals(entity.getLongName())) {
					entity.setFrequency(entity.getFrequency()+supple.getFrequency());
					entity.setAmountPay(entity.getAmountPay().add(supple.getAmountPay()));
					flag = false;
					break;
				}
				flag = true;
			}
			if (flag) baseList.add(supple);
		}
	}
	
	
}
